package ch6;

import java.util.Scanner;

public class GetAvg2 {

	public static void main(String[] args) {
		// 获得人数，再输入成绩，最后求和，算平均
		Scanner in = new Scanner(System.in);
		System.out.println("请输入班级的人数:");
		int num = 0;
		while (true) {
			num = in.nextInt();
			if (num < 1) {
				System.out.println("请重新输入");
				continue;
			}
			break;
		}
		double d[] = new double[num];
		System.out.println("请输入成绩：");
		for (int i = 0; i < d.length; i++) {
			d[i] = in.nextDouble();
		}
		// 求和
		double sum = 0;
		// for (int i = 0; i < d.length; i++) {
		// sum += d[i];
		// }
		// 增强for
		for (double t : d) {// t相当于d[i];
			sum += t;
		}
		double avg = sum / d.length;// 平均
		System.out.println("平均分是：" + avg);
	}

}
